package com.yunji.base.service;

import com.yunji.base.dao.IAllianceDao;
import com.yunji.base.domain.AllianceDO;
import com.yunji.base.vo.AllianceDTO;
import com.yunji.common.enums.common.ErrorEnum;
import com.yunji.common.model.dto.LoginInfo;
import com.yunji.common.utils.TimeUtils;
import com.yunji.common.utils.bean.BeanUtils;
import com.yunji.system.context.SystemContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Arrays;
import java.util.List;

/**
 * @Description: 加盟Service业务层处理$
 * @Author: HuaAo
 * @Date: 2024-04-02$
 */
@Service
public class AllianceService {
    @Autowired
    private IAllianceDao allianceDao;

    /**
     * 查询加盟
     *
     * @param id 加盟主键
     * @return 加盟
     */
    public AllianceDO selectSAllianceById(Long id) {
        return allianceDao.getById(id);
    }


    public boolean insertSAlliance(AllianceDO sAlliance) {
        Assert.isTrue(allianceDao.selectAllianceByMemberId(sAlliance.getMemberId()) < 10, ErrorEnum.BUSINESS_212001.getText());
        Assert.isTrue(BeanUtils.isNotNull(sAlliance.getName()), ErrorEnum.CHECKED_111001.getText());
        Assert.isTrue(sAlliance.getName().length() <= 10, ErrorEnum.CHECKED_111002.getText());
        Assert.isTrue(BeanUtils.isNotNull(sAlliance.getPhone()), ErrorEnum.CHECKED_111003.getText());
        Assert.isTrue(sAlliance.getPhone().matches("1\\d{10}"), ErrorEnum.CHECKED_111004.getText());
        Assert.isTrue(BeanUtils.isNotNull(sAlliance.getSiteAddress()), ErrorEnum.CHECKED_111005.getText());
        Assert.isTrue(sAlliance.getSiteAddress().length() <= 20, ErrorEnum.CHECKED_111006.getText());
        Assert.isTrue(BeanUtils.isNotNull(sAlliance.getSellType()), ErrorEnum.CHECKED_211001.getText());
        sAlliance.setCreateTime(TimeUtils.getNowDate());
        return allianceDao.save(sAlliance);
    }

    /**
     * 修改加盟
     *
     * @param sAlliance 加盟
     * @return 结果
     */
    public boolean updateSAlliance(AllianceDO sAlliance) {
        LoginInfo user = SystemContext.getUser();
        String nickName = user.getNickname();

        if (BeanUtils.isNotNull(sAlliance.getRemark())) {
            Assert.isTrue(sAlliance.getRemark().length() <= 50, ErrorEnum.CHECKED_111007.getText());
        }

        sAlliance.setUpdateTime(TimeUtils.getNowDate());
        //如果是已入驻 就加入审核人信息
        if (sAlliance.getAllianceStatus().equals("1") || sAlliance.getAllianceStatus().equals("2")) {
            sAlliance.setCheckName(nickName);
            sAlliance.setCheckTime(TimeUtils.getNowDate());
        }
        return allianceDao.updateById(sAlliance);
    }

    /**
     * 批量删除加盟
     *
     * @param ids 需要删除的加盟主键
     * @return 结果
     */
    public boolean deleteSAllianceByIds(Long[] ids) {
        return allianceDao.removeByIds(Arrays.asList(ids));
    }

    public List<AllianceDTO> selectSAllianceDTOList(AllianceDTO alliance) {
        return allianceDao.alliancePageList(alliance);
    }

    /**
     * 查询顾客已提交次数
     *
     * @param i
     * @return
     */
    public int selectSubmitNums(long i) {
        Long l = allianceDao.selectAllianceByMemberId(i);
        return Integer.parseInt(l.toString());
    }

    /*-------------------------------------搜索栏--------------------------------------*/

    /**
     * @param dto 过滤条件
     * @Title searchTableFiled
     * @Desc 匹配表字段信息
     * @Date 2024-09-09 14:04:00.462
     * @Return List<String> 匹配结果
     */
    public List<String> searchTableFiled(AllianceDTO dto) {
        return allianceDao.searchTableFiled(dto);
    }

//    public Integer countAlliance(AllianceCountParam param) {
//        Long l = allianceDao.countAlliance(param);
//        return Integer.parseInt(l.toString());
//    }
}
